Skip to content

Added support for more test methods, including Yarn and Composer #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020

Conversation

garethr
Copy link
Collaborator

@garethr garethr commented Jul 4, 2020

This also covers support for methods optionally taking a lock file.

This also covers support for methods optionally taking a lock file.
@@ -176,6 +176,15 @@ def _test(self, path, contents=None):
"encoding": "base64",
"files": {"target": {"contents": encoded}},
}

# Some test methods carry a second file, often a lock file
if additional:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python is EAFP than LBYL therefore a Pythonic way of writing this code block would look like:

try:
    read = getattr(additional, "read", None)
    additional = additional.read()
    encoded = base64.b64encode(additional.encode()).decode()
    post_body["files"]["additional"] = {"contents": encoded}
except:
    pass

I'm aware of the fact that the code block at the top of this method is LBYL and therefore this one is too, but worth noting IMHO

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I refer the consistency vs having two stanzas with different styles. Not against someone refactoring the whole method mind. It requires some additional type hints I think.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a point there. let's take this one separately as part of a refactor (happy to take it)

@@ -164,7 +164,7 @@ def notification_settings(self):
def invite(self, email: str, admin: bool = False):
raise SnykNotImplementedError # pragma: no cover

def _test(self, path, contents=None):
def _test(self, path, contents=None, additional=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we add type hints for this method as well, like so:

def _test(self, path: str, contents: typing.IO = None, additional: typing.IO = None)

using typing.IO will cover both binary(if necessary) and text file-like objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't quite work as is, as the interface accepts file handles or strings.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying, API looks ambiguous to me. shouldn't the function calling _test be the one reading the file content and passing that on? I guess this is an inherited design smell.

worth a re-factor, though I'm sure we'll have to consider backwards compatibility here.

for instance why test_pipfile(self, content) is acting like test_pipfile_content(self, content) while what I think would be cleaner is:

"""  test_pipfile tests a pip file for issue """
def test_pipfile(self, pip_file: typing.TextIO ) -> bool:
    with open(pip_file) as f:
        self._test(contents=f.read())

Copy link

@nirfuchs nirfuchs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some suggestions to make the code more pythonic

Copy link

@nirfuchs nirfuchs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on the fence with this one as _test() is ambiguous and adding parameters that contribute to it make it even more.

worth considering a re-factor of this one ( bet backwards compatibility would be the challenge here)

@@ -164,7 +164,7 @@ def notification_settings(self):
def invite(self, email: str, admin: bool = False):
raise SnykNotImplementedError # pragma: no cover

def _test(self, path, contents=None):
def _test(self, path, contents=None, additional=None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying, API looks ambiguous to me. shouldn't the function calling _test be the one reading the file content and passing that on? I guess this is an inherited design smell.

worth a re-factor, though I'm sure we'll have to consider backwards compatibility here.

for instance why test_pipfile(self, content) is acting like test_pipfile_content(self, content) while what I think would be cleaner is:

"""  test_pipfile tests a pip file for issue """
def test_pipfile(self, pip_file: typing.TextIO ) -> bool:
    with open(pip_file) as f:
        self._test(contents=f.read())

@garethr garethr merged commit 476156b into snyk-labs:master Jul 7, 2020
@garethr garethr deleted the extend-test-methods branch September 26, 2020 06:07
lili2311 pushed a commit that referenced this pull request Oct 10, 2023
Added support for more test methods, including Yarn and Composer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants